﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using WomanMotion.WebApi.Infrastructure.DataBaseCommon;
using WomanMotion.WebApi.Repository.IRepository;

namespace WomanMotion.WebApi.Repository.Repository
{
    /// <summary>
    /// 数据库仓储基类
    /// </summary>
    public class BaseRepository<TEntity> : IBaseRepository<TEntity> where TEntity : class, new()
    {
        protected readonly ISqlSugarClient _db;

        /// <summary>
        /// 数据库仓储基类构造函数 注入数据库上下文对象
        /// </summary>
        /// <param name="db"></param>
        public BaseRepository(ISqlSugarClient db)
        {
            this._db = db;
        }

        /// <summary>
        /// 数据库上下文
        /// </summary>
        /// <returns></returns>
        public ISqlSugarClient GetDB()
        {
            return this._db;
        }

        #region 添加

        /// <summary>
        /// 插入一个实体(异步)
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public virtual async Task<TEntity> InsertAsync(TEntity entity)
        {
            var res = await _db.Insertable(entity).ExecuteReturnEntityAsync();
            return res;
        }

        /// <summary>
        /// 批量插入数据(异步)
        /// </summary>
        /// <param name="entity">数据集合</param>
        /// <returns>返回数据行数</returns>
        public virtual async Task<int> InsertAsync(List<TEntity> entity)
        {
            var res = await _db.Insertable(entity).ExecuteCommandAsync();
            return res;
        }

        #endregion 添加

        #region 删除

        /// <summary>
        /// 删除一个实体(异步)
        /// </summary>
        /// <param name="id">ID主键</param>
        /// <returns>影响行数</returns>
        public virtual async Task<int> DeleteAsync(object id)
        {
            var res = await _db.Deleteable<TEntity>().In(id).ExecuteCommandAsync();
            return res;
        }

        /// <summary>
        /// 批量删除数据(异步)
        /// </summary>
        /// <param name="ids">主键集合</param>
        /// <returns>影响行数</returns>
        public virtual async Task<int> DeleteAsync(object[] ids)
        {
            var res = await _db.Deleteable<TEntity>().In(ids).ExecuteCommandAsync();
            return res;
        }

        /// <summary>
        /// 根据条件删除数据(异步)
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public virtual async Task<int> DeleteAsync(Expression<Func<TEntity, bool>> expression)
        {
            var res = await _db.Deleteable<TEntity>().Where(expression).ExecuteCommandAsync();
            return res;
        }

        #endregion 删除

        #region 更新

        /// <summary>
        /// 更新数据(异步)
        /// </summary>
        /// <param name="entity">需要更新的实体</param>
        /// <returns>影响行数</returns>
        public virtual async Task<int> UpdateAsync(TEntity entity)
        {
            return await _db.Updateable(entity).ExecuteCommandAsync();
        }

        /// <summary>
        /// 批量更新数据(异步)
        /// </summary>
        /// <param name="entity">实体集合</param>
        /// <returns>影响行数</returns>
        public virtual async Task<int> UpdateAsync(List<TEntity> entity)
        {
            return await _db.Updateable(entity).ExecuteCommandAsync();
        }

        #endregion 更新

        #region 查询

        /// <summary>
        /// 查询单个数据(异步)
        /// </summary>
        /// <param name="expression">条件</param>
        /// <returns></returns>
        public virtual async Task<TEntity> GetAsync(Expression<Func<TEntity, bool>> expression)
        {
            return await _db.Queryable<TEntity>().FirstAsync(expression);
        }

        /// <summary>
        /// 查询单个带导航数据(异步)
        /// </summary>
        /// <param name="expression">条件</param>
        /// <param name="ignoreProperyNameList">需要忽略的导航属性</param>
        /// <returns></returns>
        public virtual async Task<TEntity> GetNavAsync(Expression<Func<TEntity, bool>> expression, params string[] ignoreProperyNameList)
        {
            var query = _db.Queryable<TEntity>();
            //添加导航查询
            if (ignoreProperyNameList != null && ignoreProperyNameList.Length > 0)
            {
                query.IncludesAllFirstLayer(ignoreProperyNameList);
            }
            else
            {
                query.IncludesAllFirstLayer();
            }
            return await query.FirstAsync(expression);
        }

        /// <summary>
        ///查询单个数据(同步)
        /// </summary>
        /// <param name="expression">条件</param>
        /// <returns></returns>
        public virtual TEntity Get(Expression<Func<TEntity, bool>> expression)
        {
            return _db.Queryable<TEntity>().First(expression);
        }

        /// <summary>
        /// 查询单个带导航数据(同步)
        /// </summary>
        /// <param name="expression">条件</param>
        /// <param name="ignoreProperyNameList">需要忽略的导航属性</param>
        /// <returns></returns>
        public virtual TEntity GetNav(Expression<Func<TEntity, bool>> expression, params string[] ignoreProperyNameList)
        {
            var query = _db.Queryable<TEntity>();
            //添加导航查询
            if (ignoreProperyNameList != null && ignoreProperyNameList.Length > 0)
            {
                query.IncludesAllFirstLayer(ignoreProperyNameList);
            }
            else
            {
                query.IncludesAllFirstLayer();
            }
            return query.First(expression);
        }

        /// <summary>
        /// 根据主键查询单个数据(异步)
        /// </summary>
        /// <param name="id">id主键</param>
        /// <returns></returns>
        public virtual async Task<TEntity> GetByIDAsync(object id)
        {
            return await _db.Queryable<TEntity>().InSingleAsync(id);
        }

        /// <summary>
        ///根据主键查询单个带导航数据(异步)
        /// </summary>
        /// <param name="id">id主键</param>
        /// <param name="ignoreProperyNameList">需要忽略的导航属性</param>
        /// <returns></returns>
        public virtual async Task<TEntity> GetByIDNavAsync(object id, params string[] ignoreProperyNameList)
        {
            var query = _db.Queryable<TEntity>();
            //添加导航查询
            if (ignoreProperyNameList != null && ignoreProperyNameList.Length > 0)
            {
                query.IncludesAllFirstLayer(ignoreProperyNameList);
            }
            else
            {
                query.IncludesAllFirstLayer();
            }
            return await query.InSingleAsync(id);
        }

        /// <summary>
        ///根据主键查询单个数据(同步)
        /// </summary>
        /// <param name="id">id主键</param>
        /// <returns></returns>
        public virtual TEntity GetByID(object id)
        {
            return _db.Queryable<TEntity>().InSingle(id);
        }

        /// <summary>
        /// 根据主键查询单个带导航数据(同步)
        /// </summary>
        /// <param name="id">id主键</param>
        /// <param name="ignoreProperyNameList">需要忽略的导航属性</param>
        /// <returns></returns>
        public virtual TEntity GetByIDNav(object id, params string[] ignoreProperyNameList)
        {
            var query = _db.Queryable<TEntity>();
            //添加导航查询
            if (ignoreProperyNameList != null && ignoreProperyNameList.Length > 0)
            {
                query.IncludesAllFirstLayer(ignoreProperyNameList);
            }
            else
            {
                query.IncludesAllFirstLayer();
            }
            return query.InSingle(id);
        }

        /// <summary>
        /// 返回所有数据集合(异步)
        /// </summary>
        /// <returns></returns>
        public virtual async Task<List<TEntity>> GetListAsync()
        {
            return await _db.Queryable<TEntity>().ToListAsync();
        }

        /// <summary>
        /// 返回所有带排序数据集合(异步)
        /// </summary>
        /// <param name="orderby">排序数组字符串</param>
        /// <returns></returns>
        public virtual async Task<List<TEntity>> GetListOrderByAsync(string[] orderby)
        {
            var query = _db.Queryable<TEntity>();
            if (orderby != null && orderby.Length > 0)//当排序集合不为空
            {
                foreach (var item in orderby)
                {
                    query.OrderBy(item.ToSqlFilter());//格式 id asc或者 id desc
                }
            }
            return await query.ToListAsync();
        }

        /// <summary>
        /// 返回所有带导航数据集合(异步)
        /// </summary>
        /// <param name="ignoreProperyNameList">需要忽略的导航属性</param>
        /// <returns></returns>
        public virtual async Task<List<TEntity>> GetListNavAsync(params string[] ignoreProperyNameList)
        {
            var query = _db.Queryable<TEntity>();
            //添加导航查询
            if (ignoreProperyNameList != null && ignoreProperyNameList.Length > 0)
            {
                query.IncludesAllFirstLayer(ignoreProperyNameList);
            }
            else
            {
                query.IncludesAllFirstLayer();
            }
            return await query.ToListAsync();
        }

        /// <summary>
        ///返回所有带导航带排序数据集合(异步)
        /// </summary>
        /// <param name="orderby">排序数组字符串</param>
        /// <param name="ignoreProperyNameList">需要忽略的导航属性</param>
        /// <returns></returns>
        public virtual async Task<List<TEntity>> GetListNavOrderByAsync(string[] orderby, params string[] ignoreProperyNameList)
        {
            var query = _db.Queryable<TEntity>();
            //添加导航查询
            if (ignoreProperyNameList != null && ignoreProperyNameList.Length > 0)
            {
                query.IncludesAllFirstLayer(ignoreProperyNameList);
            }
            else
            {
                query.IncludesAllFirstLayer();
            }
            if (orderby != null && orderby.Length > 0)//当排序集合不为空
            {
                foreach (var item in orderby)
                {
                    query.OrderBy(item.ToSqlFilter());//格式 id asc或者 id desc
                }
            }
            return await query.ToListAsync();
        }

        /// <summary>
        /// 按条件返回数据集合(异步)
        /// </summary>
        /// <param name="expression">条件</param>
        /// <returns></returns>
        public virtual async Task<List<TEntity>> GetListAsync(Expression<Func<TEntity, bool>> expression)
        {
            return await _db.Queryable<TEntity>().Where(expression).ToListAsync();
        }

        /// <summary>
        /// 按条件返回带排序数据集合(异步)
        /// </summary>
        /// <param name="expression">条件</param>
        /// <param name="orderby">排序数组字符串</param>
        /// <returns></returns>
        public virtual async Task<List<TEntity>> GetListOrderByAsync(Expression<Func<TEntity, bool>> expression, string[] orderby)
        {
            var query = _db.Queryable<TEntity>();
            query.Where(expression);
            if (orderby != null && orderby.Length > 0)//当排序集合不为空
            {
                foreach (var item in orderby)
                {
                    query.OrderBy(item.ToSqlFilter());//格式 id asc或者 id desc
                }
            }
            return await query.ToListAsync();
        }

        /// <summary>
        /// 按条件返回带导航数据集合(异步)
        /// </summary>
        /// <param name="expression">条件</param>
        /// <param name="ignoreProperyNameList">需要忽略的导航属性</param>
        /// <returns></returns>
        public virtual async Task<List<TEntity>> GetListNavAsync(Expression<Func<TEntity, bool>> expression, params string[] ignoreProperyNameList)
        {
            var query = _db.Queryable<TEntity>();
            //添加导航查询
            if (ignoreProperyNameList != null && ignoreProperyNameList.Length > 0)
            {
                query.IncludesAllFirstLayer(ignoreProperyNameList);
            }
            else
            {
                query.IncludesAllFirstLayer();
            }
            return await query.Where(expression).ToListAsync();
        }

        /// <summary>
        ///按条件返回带排序带导航数据集合(异步)
        /// </summary>
        /// <param name="expression">条件</param>
        /// <param name="orderby">排序数组字符串</param>
        /// <param name="ignoreProperyNameList">需要忽略的导航属性</param>
        /// <returns></returns>
        public virtual async Task<List<TEntity>> GetListNavOrderByAsync(Expression<Func<TEntity, bool>> expression, string[] orderby, params string[] ignoreProperyNameList)
        {
            var query = _db.Queryable<TEntity>();
            //添加导航查询
            if (ignoreProperyNameList != null && ignoreProperyNameList.Length > 0)
            {
                query.IncludesAllFirstLayer(ignoreProperyNameList);
            }
            else
            {
                query.IncludesAllFirstLayer();
            }
            if (orderby != null && orderby.Length > 0)//当排序集合不为空
            {
                foreach (var item in orderby)
                {
                    query.OrderBy(item.ToSqlFilter());//格式 id asc或者 id desc
                }
            }
            return await query.Where(expression).ToListAsync();
        }

        /// <summary>
        /// 查询是否有符合记录的数据存在(异步)
        /// </summary>
        /// <param name="expression">表达式</param>
        /// <returns>存在true,不存在false</returns>
        public virtual async Task<bool> GetAnyAsync(Expression<Func<TEntity, bool>> expression)
        {
            return await _db.Queryable<TEntity>().Where(expression).AnyAsync();
        }

        /// <summary>
        /// 查询存在数据的行数(异步)
        /// </summary>
        /// <param name="expression">表达式</param>
        /// <returns>数据行数</returns>
        public virtual async Task<int> GetCountAsync(Expression<Func<TEntity, bool>> expression)
        {
            return await _db.Queryable<TEntity>().Where(expression).CountAsync();
        }

        /// <summary>
        /// 分页获取实体数据的KeyValuePair(异步)
        /// </summary>
        /// <param name="pars">分页查询对象</param>
        /// <returns>key是数据总数,value是数据集合</returns>
        public virtual async Task<KeyValuePair<int, List<TEntity>>> GetPageKeyValuePairListAsync(QueryParameterBase pars)
        {
            List<TEntity> data = new List<TEntity>();
            RefAsync<int> total = 0;
            //创建一个实体查询对象
            var query = _db.Queryable<TEntity>();
            //创建一个where条件集合，包含or and 和 具体的where条件IConditionalModel
            var keyVPList = new List<KeyValuePair<WhereType, IConditionalModel>>();
            if (pars.ConditionalList != null)//当参数的where条件不为空
            {
                foreach (var item in pars.ConditionalList)//循环参数的where条件集合
                {
                    //创建一个对象存储遍历出来的单个where条件
                    var keyVP = new KeyValuePair<WhereType, IConditionalModel>(item.Key, new ConditionalModel()
                    {
                        ConditionalType = item.Value.ConditionalType,
                        FieldName = item.Value.FieldName,
                        FieldValue = item.Value.FieldValue,
                    });
                    //把创建的where条件对象加入where条件集合对象
                    keyVPList.Add(keyVP);
                }
                //创建一个ConditionalTree对象 把where条件集合对象赋值ConditionalList
                IConditionalModel model = new ConditionalTree()
                {
                    ConditionalList = keyVPList
                };
                List<IConditionalModel> conditionalModels = new List<IConditionalModel>();
                conditionalModels.Add(model);
                //传入where条件到查询对象
                var list = query.Where(conditionalModels);
            }
            if (pars.OrderBys != null)//当排序集合不为空
            {
                foreach (var item in pars.OrderBys)
                {
                    query.OrderBy(item.ToSqlFilter());//格式 id asc或者 id desc
                }
            }
            data = await query.ToPageListAsync(pars.PageNum, pars.PageSize, total);
            var kv = new KeyValuePair<int, List<TEntity>>(total, data);
            return kv;
        }

        /// <summary>
        /// 分页获取包含导航实体数据的KeyValuePair(异步)
        /// </summary>
        /// <param name="pars">分页查询对象</param>
        /// <param name="ignoreProperyNameList">需要忽略的导航属性</param>
        /// <returns>key是数据总数,value是数据集合</returns>
        public virtual async Task<KeyValuePair<int, List<TEntity>>> GetPageNavKeyValuePairListAsync(QueryParameterBase pars, params string[] ignoreProperyNameList)
        {
            List<TEntity> data = new List<TEntity>();
            RefAsync<int> total = 0;
            //创建一个实体查询对象
            var query = _db.Queryable<TEntity>();
            //添加导航查询
            if (ignoreProperyNameList != null && ignoreProperyNameList.Length > 0)
            {
                query.IncludesAllFirstLayer(ignoreProperyNameList);
            }
            else
            {
                query.IncludesAllFirstLayer();
            }
            //创建一个where条件集合，包含or and 和 具体的where条件IConditionalModel
            var keyVPList = new List<KeyValuePair<WhereType, IConditionalModel>>();
            if (pars.ConditionalList != null)//当参数的where条件不为空
            {
                foreach (var item in pars.ConditionalList)//循环参数的where条件集合
                {
                    //创建一个对象存储遍历出来的单个where条件
                    var keyVP = new KeyValuePair<WhereType, IConditionalModel>(item.Key, new ConditionalModel()
                    {
                        ConditionalType = item.Value.ConditionalType,
                        FieldName = item.Value.FieldName,
                        FieldValue = item.Value.FieldValue,
                    });
                    //把创建的where条件对象加入where条件集合对象
                    keyVPList.Add(keyVP);
                }
                //创建一个ConditionalTree对象 把where条件集合对象赋值ConditionalList
                IConditionalModel model = new ConditionalTree()
                {
                    ConditionalList = keyVPList
                };
                List<IConditionalModel> conditionalModels = new List<IConditionalModel>();
                conditionalModels.Add(model);
                //传入where条件到查询对象
                var list = query.Where(conditionalModels);
            }
            if (pars.OrderBys != null)//当排序集合不为空
            {
                foreach (var item in pars.OrderBys)
                {
                    query.OrderBy(item.ToSqlFilter());//格式 id asc或者 id desc
                }
            }
            data = await query.ToPageListAsync(pars.PageNum, pars.PageSize, total);
            var kv = new KeyValuePair<int, List<TEntity>>(total, data);
            return kv;
        }

        /// <summary>
        /// 分页查询单表数据(异步)
        /// </summary>
        /// <param name="pars">查询参数</param>
        /// <returns>通用返回结果</returns>
        public virtual async Task<PageViewBase<List<TEntity>>> GetPageListAsync(QueryParameterBase pars)
        {
            PageViewBase<List<TEntity>> resList = new PageViewBase<List<TEntity>>();
            List<TEntity> data = new List<TEntity>();
            RefAsync<int> total = 0;
            //创建一个实体查询对象
            var query = _db.Queryable<TEntity>();
            //创建一个where条件集合，包含or and 和 具体的where条件IConditionalModel
            var keyVPList = new List<KeyValuePair<WhereType, IConditionalModel>>();
            if (pars.ConditionalList != null)//当参数的where条件不为空
            {
                foreach (var item in pars.ConditionalList)//循环参数的where条件集合
                {
                    //创建一个对象存储遍历出来的单个where条件
                    var keyVP = new KeyValuePair<WhereType, IConditionalModel>(item.Key, new ConditionalModel()
                    {
                        ConditionalType = item.Value.ConditionalType,
                        FieldName = item.Value.FieldName,
                        FieldValue = item.Value.FieldValue,
                        CSharpTypeName = item.Value.CSharpTypeName
                    });
                    //把创建的where条件对象加入where条件集合对象
                    keyVPList.Add(keyVP);
                }
                //创建一个ConditionalTree对象 把where条件集合对象赋值ConditionalList
                IConditionalModel model = new ConditionalTree()
                {
                    ConditionalList = keyVPList
                };
                List<IConditionalModel> conditionalModels = new List<IConditionalModel>();
                conditionalModels.Add(model);
                //传入where条件到查询对象
                var list = query.Where(conditionalModels);
            }
            if (pars.OrderBys != null)//当排序集合不为空
            {
                foreach (var item in pars.OrderBys)
                {
                    query.OrderBy(item.ToSqlFilter());//格式 id asc或者 id desc
                }
            }
            data = await query.ToPageListAsync(pars.PageNum, pars.PageSize, total);
            resList.Total = total;
            resList.PageNum = pars.PageNum;
            resList.PageSize = pars.PageSize;
            resList.Data = data;
            return resList;
        }

        /// <summary>
        /// 分页查询单表带导航数据(异步)
        /// </summary>
        /// <param name="pars">查询参数</param>
        /// <param name="ignoreProperyNameList">需要忽略的导航属性</param>
        /// <returns>通用返回结果</returns>
        public virtual async Task<PageViewBase<List<TEntity>>> GetPageNavListAsync(QueryParameterBase pars, params string[] ignoreProperyNameList)
        {
            PageViewBase<List<TEntity>> resList = new PageViewBase<List<TEntity>>();
            List<TEntity> data = new List<TEntity>();
            RefAsync<int> total = 0;
            //创建一个实体查询对象
            var query = _db.Queryable<TEntity>();
            //添加导航查询
            if (ignoreProperyNameList != null && ignoreProperyNameList.Length > 0)
            {
                query.IncludesAllFirstLayer(ignoreProperyNameList);
            }
            else
            {
                query.IncludesAllFirstLayer();
            }
            //创建一个where条件集合，包含or and 和 具体的where条件IConditionalModel
            var keyVPList = new List<KeyValuePair<WhereType, IConditionalModel>>();
            if (pars.ConditionalList != null)//当参数的where条件不为空
            {
                foreach (var item in pars.ConditionalList)//循环参数的where条件集合
                {
                    //创建一个对象存储遍历出来的单个where条件
                    var keyVP = new KeyValuePair<WhereType, IConditionalModel>(item.Key, new ConditionalModel()
                    {
                        ConditionalType = item.Value.ConditionalType,
                        FieldName = item.Value.FieldName,
                        FieldValue = item.Value.FieldValue,
                        CSharpTypeName = item.Value.CSharpTypeName
                    });
                    //把创建的where条件对象加入where条件集合对象
                    keyVPList.Add(keyVP);
                }
                //创建一个ConditionalTree对象 把where条件集合对象赋值ConditionalList
                IConditionalModel model = new ConditionalTree()
                {
                    ConditionalList = keyVPList
                };
                List<IConditionalModel> conditionalModels = new List<IConditionalModel>();
                conditionalModels.Add(model);
                //传入where条件到查询对象
                var list = query.Where(conditionalModels);
            }
            if (pars.OrderBys != null)//当排序集合不为空
            {
                foreach (var item in pars.OrderBys)
                {
                    query.OrderBy(item.ToSqlFilter());//格式 id asc或者 id desc
                }
            }
            data = await query.ToPageListAsync(pars.PageNum, pars.PageSize, total);
            resList.Total = total;
            resList.PageNum = pars.PageNum;
            resList.PageSize = pars.PageSize;
            resList.Data = data;
            return resList;
        }

        /// <summary>
        /// 多表连接分页查询(异步)
        /// </summary>
        /// <typeparam name="ViewModel"></typeparam>
        /// <param name="query">查询对象</param>
        /// <param name="pars">where 条件</param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public virtual async Task<PageViewBase<List<ViewModel>>> GetCommonPageListAsync<ViewModel>(ISugarQueryable<ViewModel> query, QueryParameterBase pars)
        {
            PageViewBase<List<ViewModel>> resList = new PageViewBase<List<ViewModel>>();
            RefAsync<int> total = 0;
            var keyVPList = new List<KeyValuePair<WhereType, IConditionalModel>>();
            if (pars.ConditionalList != null)
            {
                foreach (var item in pars.ConditionalList)
                {
                    var keyVP = new KeyValuePair<WhereType, IConditionalModel>(item.Key, new ConditionalModel()
                    {
                        ConditionalType = item.Value.ConditionalType,
                        FieldName = item.Value.FieldName,
                        FieldValue = item.Value.FieldValue,
                        CSharpTypeName = item.Value.CSharpTypeName
                    });
                    keyVPList.Add(keyVP);
                }
                IConditionalModel model = new ConditionalTree()
                {
                    ConditionalList = keyVPList
                };
                List<IConditionalModel> conditionalModels = new List<IConditionalModel>();
                conditionalModels.Add(model);
                var list = query.Where(conditionalModels);
            }
            if (pars.OrderBys != null)
            {
                foreach (var item in pars.OrderBys)
                {
                    query.OrderBy(item.ToSqlFilter());//格式 id asc或者 id desc
                }
            }

            var result = await query.ToPageListAsync(pars.PageNum, pars.PageSize, total);
            resList.Total = total;
            resList.PageNum = pars.PageNum;
            resList.PageSize = pars.PageSize;
            resList.Data = result;
            return resList;
        }

        /// <summary>
        /// 查询多表连接单条数据(异步)
        /// </summary>
        /// <typeparam name="ViewModel">表连接字段</typeparam>
        /// <param name="query">查询条件</param>
        /// <returns>多表连接VM</returns>
        public virtual async Task<ViewModel> GetCommonFirstAsync<ViewModel>(ISugarQueryable<ViewModel> query)
        {
            return await query.FirstAsync();
        }

        /// <summary>
        ///查询多表连接单条数据(同步)
        /// </summary>
        /// <typeparam name="ViewModel">表连接字段</typeparam>
        /// <param name="query">查询条件</param>
        /// <returns>多表连接VM</returns>
        public virtual ViewModel GetCommonFirst<ViewModel>(ISugarQueryable<ViewModel> query)
        {
            return query.First();
        }

        /// <summary>
        /// 查询多表连接数据集合(异步)
        /// </summary>
        /// <typeparam name="ViewModel"></typeparam>
        /// <param name="query"></param>
        /// <returns></returns>
        public virtual async Task<List<ViewModel>> GetCommonListAsync<ViewModel>(ISugarQueryable<ViewModel> query)
        {
            return await query.ToListAsync();
        }

        #endregion 查询
    }
}